﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    public class CompanyBaseDataPump : ICompanyBaseAccess
    {
        public int QueryCompanyId(string copName)
        {
            return 1;
        }

        public string QueryCompanyName(int id)
        {
            return "lawfirm";
        }

        public string QueryCompanyTempleteBasePath(int id)
        {
            return "WebTemplete\\SystemStyle\\LawFirm\\";
        }

        public string QueryCompanyTempleteBasePath(string copName)
        {
            return "WebTemplete\\SystemStyle\\LawFirm\\";
        }
        public string GetCompanyNameByMasterName(string username)
        {
            using (ORM.mysql_empiresEntities ctx = new ORM.mysql_empiresEntities())
            {
                var cop = ctx.tb_company.Where(c => c.c_vc_master == username).FirstOrDefault();
                if (cop != null)
                {
                    return cop.c_nvc_name;
                }
                return string.Empty;
            }
        }
        /// <summary>
        /// 更新网站模板
        /// </summary>
        /// <param name="master">网站超级管理员</param>
        /// <param name="styleId">模板id</param>
        /// <returns></returns>
        public int UpdateCompanyTempleteStyle(string master, int styleId)
        {
            using (ORM.mysql_empiresEntities ctx = new ORM.mysql_empiresEntities())
            {
                ctx.tb_companyconfig.Where(c => c.c_vc_cpname ==
                        ctx.tb_company.Where(o => o.c_vc_master == master)
                        .FirstOrDefault()
                        .c_vc_cpname)
                    .FirstOrDefault()
                    .c_i_style = styleId;
                //string copname = (from cop in ctx.tb_company
                //                 where cop.c_vc_master == master
                //                 select cop.c_vc_cpname).FirstOrDefault();
                //var style = (from cfg in ctx.tb_companyconfig
                //            where cfg.c_vc_cpname == copname
                //            select cfg).FirstOrDefault();
                //style.c_i_style = styleId;
                return ctx.SaveChanges();
            }
        }

        public string GetMasterNameByCopName(string copname)
        {
            using (ORM.mysql_empiresEntities ctx = new ORM.mysql_empiresEntities())
            {
                return ctx.tb_company.Where(c => c.c_vc_cpname == copname)
                    .Select(c => c.c_vc_master).FirstOrDefault();
            }
        }


        public List<DefaultDataPack> GetCompanyMemberShip(string copname, string username)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                List<DefaultDataPack> result = new List<DefaultDataPack>();
                var data = (from c in ctx.tb_membership
                              where c.c_vc_cpname == copname && c.c_vc_username == username && !c.c_i_isRefuse
                              select new { c.c_i_group, c.c_i_subgroup }).FirstOrDefault();
                if (data != null)
                {
                    result.Add(new DefaultDataPack()
                    {
                        GROUP = data.c_i_group,
                        SUBGROUP = data.c_i_subgroup
                    });
                }
                return result;
            }
        }
    }
}
